import 'package:flutter/material.dart';
import 'package:flutter_kit_plus/flutter_kit.dart';

import '../../tool/assets.dart';

class SearchView extends BaseDialog {
  SearchView({super.key, super.contentColor = Colors.black});

  @override
  SearchViewState createState() => SearchViewState();
  @override
  Future<String?> show(BuildContext context, {Function()? completion}) {
    return super.show(context, completion: completion);
  }
}

class SearchViewState<T extends SearchView> extends BaseDialogState<T> implements BLESearchTableViewDelegate {
  BLESearchTableView? _tableView;

  @override
  void initState() {
    super.initState();
    _tableView = BLESearchTableView(
      filterName: const ['B'],
      agreement: this,
      contentColor: Color_bg_view2,
      bgViewForHeader: (child) => Container(color: Colors.transparent, child: child),
      bgViewForFooter: (child) => Container(color: Colors.transparent, child: child),
    );
  }

  BoxDecoration border(Color color, {Color? borderColor}) {
    return BoxDecoration(
      color: color,
      border: Border.all(color: borderColor ?? color, width: 0.5),
      borderRadius: BorderRadius.circular(10),
      boxShadow: const [BoxShadow(color: Colors.black, offset: Offset(0, 0), blurRadius: 20, spreadRadius: 0)],
    );
  }

  Widget getHeader(String title, {void Function()? ontap}) {
    return SizedBox(
      height: 40,
      child: Center(
        child: InkWell(
          child: Text(title),
          onTap: () => ontap?.call(),
        ),
      ),
    );
  }

  @override
  Widget buildContent(BuildContext context) {
    return Container(
      padding: const EdgeInsets.only(left: 5, bottom: 5, right: 5, top: 0),
      width: 600,
      decoration: border(Colors.black, borderColor: Colors.white),
      child: Column(
        children: [
          getHeader('Settings'),
          Expanded(child: Container(decoration: border(Color_bg_view2), child: _tableView)),
          getHeader(
            'Refresh Device List',
            ontap: () => _tableView!.refreshKey?.currentState?.show(),
          ),
        ],
      ),
    );
  }

  @override
  List<RowModel> listModelForHeader(String appVersion) {
    return [];
  }

  @override
  List<RowModel> listModelForFooter(String appVersion) {
    return [];
  }

  @override
  List<RowModel> listModelForSection(List<String> list) {
    return [];
  }

  @override
  void methodForClickBLESearchTableView(BLESearchTableViewClickType type, String tag) {
    switch (type) {
      case BLESearchTableViewClickType.header:
        break;
      case BLESearchTableViewClickType.footer:
        break;
      case BLESearchTableViewClickType.section:
        break;
    }
  }

  @override
  Widget viewForRowInSection(DSPeripheral peripheral, int section, int row, bool isOn) {
    bool connect = peripheral.peripheral?.result.device.isConnected ?? false;
    return Container(
      height: 40,
      padding: EdgeInsets.only(left: 10, right: 10),
      child: Column(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Expanded(
            child: ElevatedButton(
              onPressed: () {},
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                spacing: 10,
                children: [
                  Expanded(
                    child: FittedBox(
                      fit: BoxFit.scaleDown,
                      alignment: Alignment.centerLeft,
                      child: Text(
                        peripheral.peripheral!.result.device.advName,
                        maxLines: 1,
                      ),
                    ),
                  ),
                  Text(connect ? 'Connected' : 'Disconnected',
                      style: TextStyle(color: connect ? Color_theme : Color_red)),
                ],
              ),
            ),
          ),
          Container(
            height: (_tableView?.deviceList[section].values.first.length ?? 0) - 1 == row ? 0 : 0.5,
            color: Colors.white,
          ),
        ],
      ),
    );
  }
}
